﻿/*
	Copyright (C) 2009 - 2012
	WebSite:	Http://wangking717.javaeye.com/
	Author:		wangking
*/
jQuery(function($){
	var xOffset = -20; // x distance from mouse
    var yOffset = 20; // y distance from mouse  
	
	//input tips
	$("[tip]").hover(
		function(e) {
			if($(this).attr('tip') != undefined){
				var top = (e.pageY + yOffset);
				var left = (e.pageX + xOffset);
				//var img_src = "resources/js/jQuery/plugins/validator/vtip_arrow1.png";
				$('body').append("<p id='vtip'>" + $(this).attr('tip') + "</p>");
				$('p#vtip').css("top", top+"px").css("left", left+"px");
				$('p#vtip').bgiframe();
			}
		},
		function() {
			if($(this).attr('tip') != undefined){
				$("p#vtip").remove();
			}
		}
	).mousemove(
		function(e) {
			if($(this).attr('tip') != undefined){
				var top = (e.pageY + yOffset);
				var left = (e.pageX + xOffset);
				$("p#vtip").css("top", top+"px").css("left", left+"px");
			}
		}
	);
	
	$("[reg],[url]:not([reg])").blur(function(){
		if($(this).attr("reg") != undefined){
			reg_validate($(this));
		}
		if($(this).attr("url") != undefined){
			ajax_validate($(this));
		}
	});
	
	$("form").submit(function(){
		return WEB.Form.validator(this);
	});
	
	/*----------ct.wen provided regular expression syntax reference-----------------*/
	function reg_validate(obj){
		var var_val = obj.attr("value");
		var var_regs = obj.attr("reg").split("|");
		var var_count = 0;
		var var_valid = false;
		
		for (var ie in var_regs) {
			switch (var_regs[ie]) {
			case "e_mail":
				var_valid = /^[A-Za-z0-9][-\.\w]{1,30}[A-Za-z0-9]@([A-Za-z0-9][-\w]{0,62}\.){1,3}[A-Za-z0-9]{2,4}$/.test(var_val);  //E-Mail Address matching
				if(var_valid) var_valid = /[\S+\.{2,}]+/.test(var_val); //Can not be consecutive symbols (.)
				break;
			case "id_card":
				var_valid = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/.test(var_val); //15 id_card
				if (var_valid) var_valid = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/.test(var_val); //18 id_card
				break;
			case "url":
				var_valid = /^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w-\.\/?%&=]*)?/.test(var_val); //Match the URL
				break;
			case "lang_chinese":
				var_valid = /^[\u4e00-\u9fa5]$/.test(var_val); //Match Chinese characters(single character)
				if (var_valid) var_valid = /^[^\x00-\xff]$/.test(var_val); //Match double-byte characters(including Chinese characters, including a single character).
				break;
			case "zip_code":
				var_valid = /^[1-9]\d{5}(?!\d)$/.test(var_val); //Match zip code
				break;
			case "date":
				var_valid = /^[1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9]$/.test(var_val); //Match Date,Such As:2010-10-10.
				break;
			case "telephone":
				var_valid = /^\d{3}-\d{8}|\d{4}-\d{7,8}$/.test(var_val); //Match domestic Telephone, Such As:0739-8888888(8) Or 020-88888888
				break;
			case "mobile_telephone":
				var_valid = /^\d{3}-\d{12}$/.test(var_val); //Match Mobile Phone
				break;
			case "ipv4_address":
				var_valid = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(var_val); //Match IPv4 Address
				break;
			case "number":
				var_valid = /^\d+(\.?\d+)?$/.test(var_val); //Match Number
				break;
			case "integer":
				var_valid = /^\d+$/.test(var_val);  //Match Integer Number
				break;
			case "double":
				var_valid = /^\d+\.?\d+$/.test(var_val);  //Match Real Number
				break;
			case "money":
				var_valid = /^\d+(\.?\d+)?$/.test(var_val);  //Match Money
				break;
			case "lang_english":
				var_valid = /^[a-zA-Z]+$/.test(var_val);  //Match English
				break;
			default:
				var_valid = new RegExp(var_regs[ie]).test(var_val); //Customer Rule Match
				break;
			}
			//if match is correct, then accumulate.
			if (var_valid) var_count++;
		}
		//if match all the rules, was true, otherwise false;
		var_valid = var_count == var_regs.length;
		if(var_valid) {
			change_error_style(obj,"remove");
			change_tip(obj,null,"remove");
		} else {
			change_error_style(obj,"add");
			change_tip(obj,null,"add");
		}
		return var_valid;
	}

	function ajax_validate(obj){
		var url_str = obj.attr("url");
		if(url_str.indexOf("?") != -1){
			url_str = url_str+"&"+obj.attr("name")+"="+obj.attr("value");
		}else{
			url_str = url_str+"?"+obj.attr("name")+"="+obj.attr("value");
		}
		var feed_back = $.ajax({url: url_str,cache: false,async: false}).responseText;
		feed_back = feed_back.replace(/(^\s*)|(\s*$)/g, "");
		if(feed_back == 'success'){
			change_error_style(obj,"remove");
			change_tip(obj,null,"remove");
			return true;
		}else{
			change_error_style(obj,"add");
			change_tip(obj,feed_back,"add");
			return false;
		}
	}

	function change_tip(obj,msg,action_type){
		if(obj.attr("tip") == undefined){
			obj.attr("is_tip_null","yes");
		}
		if(action_type == "add"){
			if(obj.attr("is_tip_null") == "yes"){
				obj.attr("tip",msg);
			}else{
				if(msg != null){
					if(obj.attr("tip_bak") == undefined){
						obj.attr("tip_bak",obj.attr("tip"));
						obj.attr("tip",msg);
					}
				}
			}
		}else{
			if(obj.attr("is_tip_null") == "yes"){
				obj.removeAttr("tip");
				obj.removeAttr("tip_bak");
			}else{
				obj.attr("tip",obj.attr("tip_bak"));
				obj.removeAttr("tip_bak");
			}
		}
	}

	function change_error_style(obj,action_type){
		if(action_type == "add"){
			obj.addClass("input_validation-failed");
		}else{
			obj.removeClass("input_validation-failed");
		}
	}

	$.extend(WEB.Form, { // 扩展框架，表单。
		validator: function(form){ // 验证。
			var isSubmit = true;
			$(form).find("[reg],[url]:not([reg])").each(function(){
				if($(this).attr("reg") != undefined){
					if(!reg_validate($(this))) isSubmit = false;
				}
				if($(this).attr("url") != undefined){
					if(!ajax_validate($(this))) isSubmit = false;
				}
			});
			/*if (isSubmit) { 
				$(".auto").each(function(i, e){ // clear autoNumeric format.
					$e = $(e);
					$e.val($e.val().replace(/[^\d\.]/g,''));
				}); 
				var $password = $('#password'); // Encrypt the plaintext(password).
				if($password.length > 0) $password.val(hex_md5($password.val()).toUpperCase());
			}*/
			return isSubmit;
		}
	});
});